package com.chan.web.controller.cd;

import java.util.List;

import com.chan.common.annotation.Log;
import com.chan.common.core.controller.BaseController;
import com.chan.common.core.domain.AjaxResult;
import com.chan.common.core.domain.Ztree;
import com.chan.common.enums.BusinessType;
import com.chan.common.utils.StringUtils;
import com.chan.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chan.domain.CdProgram;
import com.chan.service.ICdProgramService;


/**
 * 项目集Controller
 *
 * @author chan
 * @date 2024-04-08
 */
@Controller
@RequestMapping("/cd/program")
public class CdProgramController extends BaseController
{
    private String prefix = "cd/program";

    @Autowired
    private ICdProgramService cdProgramService;

    @RequiresPermissions("cd:program:view")
    @GetMapping()
    public String program()
    {
        return prefix + "/program";
    }

    /**
     * 查询项目集树列表
     */
    @RequiresPermissions("cd:program:list")
    @PostMapping("/list")
    @ResponseBody
    public List<CdProgram> list(CdProgram cdProgram)
    {
        List<CdProgram> list = cdProgramService.selectCdProgramList(cdProgram);
        return list;
    }

    /**
     * 导出项目集列表
     */
    @RequiresPermissions("cd:program:export")
    @Log(title = "项目集", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CdProgram cdProgram)
    {
        List<CdProgram> list = cdProgramService.selectCdProgramList(cdProgram);
        ExcelUtil<CdProgram> util = new ExcelUtil<CdProgram>(CdProgram.class);
        return util.exportExcel(list, "项目集数据");
    }

    /**
     * 新增项目集
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) Integer id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("cdProgram", cdProgramService.selectCdProgramById(id));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存项目集
     */
    @RequiresPermissions("cd:program:add")
    @Log(title = "项目集", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CdProgram cdProgram)
    {
        return toAjax(cdProgramService.insertCdProgram(cdProgram));
    }

    /**
     * 修改项目集
     */
    @RequiresPermissions("cd:program:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        CdProgram cdProgram = cdProgramService.selectCdProgramById(id);
        mmap.put("cdProgram", cdProgram);
        return prefix + "/edit";
    }

    /**
     * 修改保存项目集
     */
    @RequiresPermissions("cd:program:edit")
    @Log(title = "项目集", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CdProgram cdProgram)
    {
        return toAjax(cdProgramService.updateCdProgram(cdProgram));
    }

    /**
     * 删除
     */
    @RequiresPermissions("cd:program:remove")
    @Log(title = "项目集", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Integer id)
    {
        return toAjax(cdProgramService.deleteCdProgramById(id));
    }

    /**
     * 选择项目集树
     */
    @GetMapping(value = { "/selectProgramTree/{id}", "/selectProgramTree/" })
    public String selectProgramTree(@PathVariable(value = "id", required = false) Integer id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("cdProgram", cdProgramService.selectCdProgramById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 加载项目集树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = cdProgramService.selectCdProgramTree();
        return ztrees;
    }
}
